<?php
/**
 * created by zzy
 * date: 2018/1/26 14:04
 */
namespace App\Eloquent\Ygt;

use App\Eloquent\Area;
use App\Eloquent\City;
use App\Eloquent\Province;
use Illuminate\Database\Eloquent\SoftDeletes;


class SaleCompany extends DbEloquent{

    use SoftDeletes;
    protected $dates    = ['deleted_at'];
    protected $table    = 'ygt_company';
    public static $tableFieldList   = [
        ['name'=>'id','title'=>'','value'=>0,'is_must'=>0],
        ['name'=>'title','title'=>'厂名称','value'=>'','is_must'=>1],
        ['name'=>'mobile','title'=>'手机号','value'=>'','is_must'=>1],
        ['name'=>'province_id','title'=>'厂地址','value'=>0,'is_must'=>1],
        ['name'=>'city_id','title'=>'厂地址','value'=>0,'is_must'=>1],
        ['name'=>'area_id','title'=>'厂地址','value'=>0,'is_must'=>1],
        ['name'=>'address','title'=>'厂地址','value'=>'','is_must'=>0],
    ];

    public static function edit($data,$pk='')
    {
        $id                 = isset($data['id']) ? intval($data['id']) : 0;
        $provinceId         = $data['province_id'];
        $cityId             = $data['city_id'];
        $areaId             = $data['area_id'];
        $title              = $data['title'];
        $pinyin             = app('pinyin')->abbr($title);
        $firstLetter        = strtoupper(substr($pinyin,0,1));
        $letterAll          = [];
        for($i=65;$i<91;$i++){
            $letterAll[]    = chr($i);
        }
        if(!in_Array($firstLetter,$letterAll))
        {
            $firstLetter        = '#';
        }
        $data['first_letter']   = $firstLetter;
        $provinceName       = $cityName = $areaName = '';
        if($provinceId != 0)
        {
            $where          = ['provinceID'=>$provinceId];
            $collection     = Province::where($where)->first();
            $provinceName   = $collection->province;
        }
        if($cityId != 0)
        {
            $where          = ['cityID'=>$cityId];
            $collection     = City::where($where)->first();
            $cityName       = $collection->city;
        }
        if($areaId != 0)
        {
            $where          = ['areaID'=>$areaId];
            $collection     = Area::where($where)->first();
            $areaName       = $collection->area;
        }
        $data['province_name']  = $provinceName;
        $data['city_name']      = $cityName;
        $data['area_name']      = $areaName;
        if($id>0){
            $where          = ['id'=>$id];
            $result         = self::updateOneData($where, $data,$pk);
        } else {
            $result         = self::insertOneData($data,$pk);
        }
        return $result;
    }
    public static function getListJoinRelation($whereArr,$fields='',$limit=0,$offset=0)
    {
        $orderby            = ['ygt_company.id','desc'];

        $fieldL             = 'ygt_sale_company_relation.sale_company_id';
        $fieldR             = 'ygt_company.id';
        $fieldRcustomer     = 'ygt_sale_company_relation.customer_id';

        $fieldDefault       = 'ygt_company.id,ygt_company.title,ygt_company.mobile';
        $fieldDefault       .= ',ygt_company.province_id,ygt_company.city_id';
        $fieldDefault       .= ',ygt_company.area_id,ygt_company.province_name';
        $fieldDefault       .= ',ygt_company.city_name,ygt_company.area_name';
        $fieldDefault       .= ',ygt_company.address,ygt_company.first_letter';
        $fieldDefault       .= ',ygt_sale_company_relation.customer_id';
        $fieldDefault       .= ',ygt_customer.customer_name';
        $fields             = $fields == '' ? $fieldDefault : $fields;
        $join               = [
            ['table'=>'ygt_sale_company_relation', 'field_l'=>$fieldL, 'field_c'=>'=', 'field_r'=>$fieldR],
            ['table'=>'ygt_customer', 'field_l'=>'ygt_customer.id', 'field_c'=>'=', 'field_r'=>$fieldRcustomer],
        ];
        $result             = self::getList($whereArr,$fields,$limit,$offset,$orderby,'',$join);
        return $result;
    }

}
